package com.xiaoma.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
        //连接服务器，如果成功发送数据，失败可以做其它工作
        if (!socketChannel.connect(inetSocketAddress)){
              while (!socketChannel.finishConnect()){
                  System.out.println("做其它工作");
              }
        }
        //发数据
        String str ="hello xiaoma";
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        socketChannel.write(buffer);
        System.in.read();
    }
}
